<?php
/**
 *
 * Created by www.soe-soe.com
 * Author: shijy
 * Date: 2015/12/10
 * Time: 10:56
 */

function getSmarty(){
    static $smartyAdmin;
    if ($smartyAdmin) {
        return $smartyAdmin;
    }
    import('Vendor.Smarty.Smarty');
    $smartyAdmin = new Smarty();
    $smartyAdmin->template_dir = './Tpl/Shop/';
    $smartyAdmin->compile_dir = RUNTIME_PATH.'Smarty/';
    $smartyAdmin->addPluginsDir(WEB_ROOT.'/smarty_plugin');
    $smartyAdmin->caching = false;

    $smartyAdmin->left_delimiter='<{';
    $smartyAdmin->right_delimiter='}>';

    $smartyAdmin->assign('module',MODULE_NAME);
    $smartyAdmin->assign('controller',CONTROLLER_NAME);
    $smartyAdmin->assign('action',ACTION_NAME);

    $smartyAdmin->assign('preUploadUrl',C('UPLOAD_RESOURCE_PREFIX'));//
    $smartyAdmin->assign('preStaticUrl',C('STATIC_RESOURCE_PREFIX'));//

    $smartyAdmin->assign('resVersion',RESOURCE_VERSION);//静态资源版本控制

    return $smartyAdmin;
}


/**
 *根据输入的字段来判断是否存在
 *@parm filed 字段的名称
 *@parm filed_vale 字段的值
 *@parm table 数据库表的字
 */
function is_exist_filed($filed,$filed_value, $table) {
    $model = M($table);
    $data = array(
        $filed => $filed_value,
    );
    $result = $model->where($data)->select();
    if($result) {
        return true;
    }else{
        return false;
    }

}
/**
 * 获取所有二级菜单
 */
function getSecondaryMenu(){
    $item = array();
    $sysNode = M('SysNode');
    $item = $sysNode
        //354为商户id  暂时写死
        ->where('level=2 and is_menu=1 and pid=355')
        ->order('sort desc')
        ->select();
    return $item;
}

/**
 * 获取级所有三级菜单
 */
function getThirdMenu($role_id){
    $item = array();
    $sysNode = M('SysNode');
    $item = $sysNode
        ->join('gm_sys_access on gm_sys_access.node_id = gm_sys_node.id and gm_sys_access.role_id=' . $role_id)
        ->where('gm_sys_node.level=3 and gm_sys_node.path like ' . '"355/'.'%"')
        ->select();
    return $item;
}
//获取所有菜单栏的路径
function getAllPathMenu($path){
    $sysNode = M("gm_sys_node",null,DB_GM_CFG);
    $path_array = explode('/',$path);
    //去掉第一个数组 为一级路径
    array_shift($path_array);
    //去掉最后个数组 如果格式为/**/**/**/ 最后个数组为空
    array_pop($path_array);
    $path = array();
    foreach($path_array as $value) {
        $result = $sysNode->field('name')->find($value);
        $path[] = $result['name'];
    }
    $path[0] = ucfirst($path[0]);
    $path = implode('/', $path);
    return $path;
}

/**
 *  随机生成12的卡券号 存入数据库
 * @cardId 对应卡券信息表的 id
 * @data 卡券信息
 * @stock 卡券库存
 * 算法描述 卡券号 12位
 * 根据卡券信息表的id  000** 拆分为 000 **
 * 分表插入 卡券12位的 456位  11 12位 剩余的位置通过生成不重复的数据
 * ***（随机数） + ***(id前三) + **(随机数) + **(count) + **(id后二)
 */
function createRandomCardList($cardId,$data,$stock,$count)
{
    $cardlist = M('card_code', null, DB_MAIN_CFG);
    //根据cardId 如果不满5位则自动向左补5位
    $cardId = str_pad(strval($cardId), 5, 0, STR_PAD_LEFT);
    $count = str_pad(strval($count), 2, 0, STR_PAD_LEFT);
    $cardId_three = substr($cardId,0,3);
    $cardId_two = substr($cardId,3);
    //根据
    $code = array();
    //根据库存生成相对应的卡券id
    while(count($code) < $stock){
        //可以只随机一次  还要进一步优化
        $code_three = mt_rand(100,999);
        $code_two = mt_rand(10,99);
        //优化细节
        $temp = strval($code_three . $cardId_three . $code_two . $count .$cardId_two);
        $code[$temp] = 0;
    }
    $code = array_keys($code);
    $dataList = array();
    foreach($code as $key => $value){
        $dataList[] = array(
            'cardTitle' => $data['cardTitle'],
            'createTimestamp' => $data['createTimestamp'],
            'restaurantId' => $data['restaurantId'],
            'cardId'   => $data['cardId'],
            'status' => $data['status'],
            'code' => $value
        );
    }
    //批量插入数据库
   $cardlist->addAll($dataList);
}




